package com.tangbuzhi.iteraror;

import java.util.ArrayList;
import java.util.List;

/**
 * 迭代器模式案例
 */
public class IterarorCase {
    public static class Employee {
        private String name;//姓名
        private int age;//年龄
        private String sex;//性别
        private String job;//职务

        public Employee(String name, int age, String sex, String job) {
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.job = job;
        }

        @Override
        public String toString() {
            return "Emplyee{name=" + name + ",age=" + age + ",sex=" + sex + ",job=" + job + "}";
        }
    }

    public static class Min {
        private List<Employee> list = new ArrayList<>();
        private Employee lilei = new Employee("lilei", 18, "male", "job1");
        private Employee hanmeimei = new Employee("hanmeimei", 28, "female", "job2");
        private Employee lily = new Employee("lily", 24, "female", "job0");
        private Employee tantong = new Employee("tantong", 42, "male", "job0");

        public Min() {
            list.add(lilei);
            list.add(hanmeimei);
            list.add(lily);
            list.add(tantong);
        }

        public List<Employee> getList() {
            return list;
        }
    }

    public static class Hui {
        private Employee[] array = new Employee[4];
        private Employee green = new Employee("green", 39, "male", "job0");
        private Employee lucy = new Employee("lucy", 20, "female", "job1");
        private Employee lancy = new Employee("lancy", 16, "female", "job2");
        private Employee jacky = new Employee("jacky", 35, "male", "job1");

        public Hui() {
            array[0] = green;
            array[1] = lucy;
            array[2] = lancy;
            array[3] = jacky;
        }

        public Employee[] getArray() {
            return array;
        }
    }

    public static void main(String[] args) {
        Min min = new Min();
        List<Employee> list = min.getList();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }

        System.out.println("===================================================");
        Hui hui = new Hui();
        Employee[] array = hui.getArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i].toString());
        }
        /**
         Emplyee{name=lilei,age=18,sex=male,job=job1}
         Emplyee{name=hanmeimei,age=28,sex=female,job=job2}
         Emplyee{name=lily,age=24,sex=female,job=job0}
         Emplyee{name=tantong,age=42,sex=male,job=job0}
         ===================================================
         Emplyee{name=green,age=39,sex=male,job=job0}
         Emplyee{name=lucy,age=20,sex=female,job=job1}
         Emplyee{name=lancy,age=16,sex=female,job=job2}
         Emplyee{name=jacky,age=35,sex=male,job=job1}
         */

        System.out.println("==============================================================");
        CompanyMin companyMin = new CompanyMin();
        EmployeeIterator minIterator = companyMin.iterator();
        showDetail(minIterator);

        System.out.println("==============================================================");
        CompanyHui companyHui = new CompanyHui();
        EmployeeIterator huiIterator = companyHui.iterator();
        showDetail(huiIterator);
    }

    private static void showDetail(EmployeeIterator iterator) {
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toString());
        }
    }

    public interface EmployeeIterator<T> {
        boolean hasNext();

        T next();
    }

    public static class MinIterator implements EmployeeIterator<Employee> {
        private List<Employee> list;
        private int cursor = 0;

        public MinIterator(List<Employee> list) {
            this.list = list;
        }

        @Override
        public boolean hasNext() {
            return !(cursor > list.size() - 1 || list.get(cursor) == null);
        }

        @Override
        public Employee next() {
            return list.get(cursor++);
        }
    }

    public static class HuiIterator implements EmployeeIterator<Employee> {
        private Employee[] array;
        private int cursor = 0;

        public HuiIterator(Employee[] array) {
            this.array = array;
        }

        @Override
        public boolean hasNext() {
            return !(cursor > array.length - 1 || array[cursor] == null);
        }

        @Override
        public Employee next() {
            return array[cursor++];
        }
    }

    public interface Company {
        EmployeeIterator iterator();
    }

    public static class CompanyMin implements Company {
        private List<Employee> list = new ArrayList<>();
        private Employee lilei = new Employee("lilei", 18, "male", "job1");
        private Employee hanmeimei = new Employee("hanmeimei", 28, "female", "job2");
        private Employee lily = new Employee("lily", 24, "female", "job0");
        private Employee tantong = new Employee("tantong", 42, "male", "job0");

        public CompanyMin() {
            list.add(lilei);
            list.add(hanmeimei);
            list.add(lily);
            list.add(tantong);
        }

        @Override
        public EmployeeIterator iterator() {
            return new MinIterator(list);
        }
    }

    public static class CompanyHui implements Company {
        private Employee[] array = new Employee[4];
        private Employee green = new Employee("green", 39, "male", "job0");
        private Employee lucy = new Employee("lucy", 20, "female", "job1");
        private Employee lancy = new Employee("lancy", 16, "female", "job2");
        private Employee jacky = new Employee("jacky", 35, "male", "job1");

        public CompanyHui() {
            array[0] = green;
            array[1] = lucy;
            array[2] = lancy;
            array[3] = jacky;
        }

        @Override
        public EmployeeIterator iterator() {
            return new HuiIterator(array);
        }
    }
}
